﻿using System;
using System.Windows.Forms;

namespace Microsoft.WowAddonStudio.Project.OptionPages
{
    public partial class FrameXmlDesignerGeneralOptionsControl : UserControl
    {
        /// <summary>
        /// Occurs when [background image file changed].
        /// </summary>
        public event EventHandler BackgroundImageFileChanged;

        /// <summary>
        /// Gets or sets the background image file.
        /// </summary>
        /// <value>The background image file.</value>
        public string BackgroundImageFile
        {
            get { return textBoxBackgroundImage.Text; }
            set
            {
                textBoxBackgroundImage.Text = value;
                checkBoxCustomBackgroundImageFile.Checked = (value == null);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FrameXmlDesignerGeneralOptionsControl"/> class.
        /// </summary>
        public FrameXmlDesignerGeneralOptionsControl()
        {
            InitializeComponent();
           
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            textBoxBackgroundImage.TextChanged += OnTextBoxBackgroundImageTextChanged;
        }

        private void OnTextBoxBackgroundImageTextChanged(object sender, EventArgs e)
        {
            if (BackgroundImageFileChanged != null)
                BackgroundImageFileChanged(this, EventArgs.Empty);
        }

        /// <summary>
        /// Called when [click browse].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnClickBrowse(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                textBoxBackgroundImage.Text = openFileDialog.FileName;
        }

        private void checkBoxCustomBackgroundImageFile_CheckedChanged(object sender, EventArgs e)
        {
            textBoxBackgroundImage.Enabled = buttonBrowse.Enabled = checkBoxCustomBackgroundImageFile.Checked;
            if (!textBoxBackgroundImage.Enabled)
                textBoxBackgroundImage.Text = null;
        }

    }
}